package com.ruoyi.common.utils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

public class ImageZipUtil {
    public static void main(String[] args) {

//        int filesNum = numberOfFiles();
//        System.out.println("filesNum"+filesNum);
//        for (int i = 0; i<=filesNum; i++){
//            zipWidthHeightImageFile(new File("C:\\oldimage\\"+i+".jpg"),new File("C:\\newimage\\"+i+"-"+i+".jpg"),664,1000,0.7f);
//        }

        //String path = "C://oldimage";
        //getFile(path);
        zipImageFile(new File("C:\\Users\\Mr\\Desktop\\IMG_3018.PNG"), new File("C:\\Users\\Mr\\Desktop\\IMG_3018-1.PNG"), 252, 546, 0.7f);
        //zipImageFile(new File("C:\\spider\\3.jpg"),new File("C:\\spider\\3-3.jpg"),425,638,0.7f);
        System.out.println("ok");
    }

    /**
     * 根据设置的宽高等比例压缩图片文件<br> 先保存原文件，再压缩、上传
     *
     * @param oldFile 要进行压缩的文件
     * @param newFile 新文件
     * @param width   宽度 //设置宽度时（高度传入0，等比例缩放）
     * @param height  高度 //设置高度时（宽度传入0，等比例缩放）
     * @param quality 质量
     * @return 返回压缩后的文件的全路径
     */
    public static String zipImageFile(File oldFile, File newFile, int width, int height, float quality) {
        if (oldFile == null) {
            return null;
        }
        try {
            /** 对服务器上的临时文件进行处理 */
            Image srcFile = ImageIO.read(oldFile);
            int w = srcFile.getWidth(null);
            int h = srcFile.getHeight(null);
            double bili;
            if (width > 0) {
                bili = width / (double) w;
                height = (int) (h * bili);
            } else {
                if (height > 0) {
                    bili = height / (double) h;
                    width = (int) (w * bili);
                }
            }
            String srcImgPath = newFile.getAbsoluteFile().toString();
            System.out.println(srcImgPath);
            String subfix = "jpg";
            subfix = srcImgPath.substring(srcImgPath.lastIndexOf(".") + 1, srcImgPath.length());
            BufferedImage buffImg = null;
            if (subfix.equals("png")) {
                buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            } else {
                buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            }
            Graphics2D graphics = buffImg.createGraphics();
            graphics.setBackground(new Color(255, 255, 255));
            graphics.setColor(new Color(255, 255, 255));
            graphics.fillRect(0, 0, width, height);
            graphics.drawImage(srcFile.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
            ImageIO.write(buffImg, subfix, new File(srcImgPath));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return newFile.getAbsolutePath();
    }

    /**
     * 按设置的宽度高度压缩图片文件<br> 先保存原文件，再压缩、上传
     *
     * @param oldFile 要进行压缩的文件全路径
     * @param newFile 新文件
     * @param width   宽度
     * @param height  高度
     * @param quality 质量
     * @return 返回压缩后的文件的全路径
     */
    public static String zipWidthHeightImageFile(File oldFile, File newFile, int width, int height, float quality) {
        if (oldFile == null) {
            return null;
        }
        String newImage = null;
        try {
            /** 对服务器上的临时文件进行处理 */
            Image srcFile = ImageIO.read(oldFile);
            String srcImgPath = newFile.getAbsoluteFile().toString();
            System.out.println(srcImgPath);
            String subfix = "jpg";
            subfix = srcImgPath.substring(srcImgPath.lastIndexOf(".") + 1, srcImgPath.length());
            BufferedImage buffImg = null;
            if (subfix.equals("png")) {
                buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            } else {
                buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            }
            Graphics2D graphics = buffImg.createGraphics();
            graphics.setBackground(new Color(255, 255, 255));
            graphics.setColor(new Color(255, 255, 255));
            graphics.fillRect(0, 0, width, height);
            graphics.drawImage(srcFile.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
            ImageIO.write(buffImg, subfix, new File(srcImgPath));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return newImage;
    }


    public static BufferedImage yaSuoImage(Image src, int width, int height) {
        BufferedImage buffImg = null;
        /** 对服务器上的临时文件进行处理 */
        buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D graphics = buffImg.createGraphics();
        graphics.setBackground(new Color(255, 255, 255));
        graphics.setColor(new Color(255, 255, 255));
        graphics.fillRect(0, 0, width, height);
        graphics.drawImage(src.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
        return buffImg;
    }

    public static int numberOfFiles() {
        File folder = new File("C:\\oldimage");
        File[] list = folder.listFiles();
        int fileCount = 0, folderCount = 0;
        int intcount = 0;
        long length = 0;
        for (File file : list) {
            if (file.isFile()) {
                fileCount++;
                length += file.length();
            } else {
                folderCount++;
            }
            intcount = fileCount;
        }

        System.out.println("文件夹的数目: " + folderCount + " 文件的数目: " + fileCount);
        return intcount;
    }


    public static void getFile(String path) {
        // get file list where the path has
        File file = new File(path);
        // get the folder list
        File[] array = file.listFiles();

        for (int i = 0; i < array.length; i++) {
            if (array[i].isFile()) {
                // only take file name

                if ((array[i].getName() != null) && (array[i].getName().length() > 0)) {
                    int dot = array[i].getName().lastIndexOf('.');
                    if ((dot > -1) && (dot < (array[i].getName().length()))) {
                        System.out.println(array[i].getName().substring(0, dot));
                        System.out.println("../images/" + array[i].getName());
                        int filesNum = numberOfFiles();
                        System.out.println("filesNum" + filesNum);
                        for (int j = 0; j <= filesNum; j++) {
                            zipWidthHeightImageFile(new File("C:\\oldimage\\" + array[i].getName().substring(0, dot) + ".jpg"), new File("C:\\newimage\\" + array[i].getName().substring(0, dot) + ".jpg"), 664, 1000, 0.7f);
                        }
                    }
                }
//                // take file path and name
//                System.out.println("#####" + array[i]);
//                // take file path and name
//                System.out.println("*****" + array[i].getPath());
            } else if (array[i].isDirectory()) {
                getFile(array[i].getPath());
            }
        }
    }

}
